<template>
  <div class="info">
    <div class="box2">
      <el-row>
        <el-col :span="24">
          <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-dynamic">
            <el-form-item label="轮播图名称" prop="title">
              <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="轮播图图片" prop="img">
              <el-upload
                action="#"
                list-type="picture-card"
                :file-list="fileList"
                :auto-upload="false"
                :limit="1"
                :on-change="handleChange"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-radio v-model="ruleForm.status" :label="1">正常</el-radio>
              <el-radio v-model="ruleForm.status" :label="2">禁用</el-radio>
            </el-form-item>
            <el-form-item>
              <el-button v-if="popType=='修改'" type="danger" @click="submitForm('ruleForm')">立即修改</el-button>
              <el-button v-else type="success" @click="submitForm('ruleForm')">立即添加</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import { getCateList, cateEdit, cateAdd } from "../../request";
export default {
  data() {
    return {
      ruleForm: this.transData,
      // // 上传文件列表
      fileList: [],
      rules: {
        title: [
          { required: true, message: "请输入轮播图名称", trigger: "blur" }
        ]
      }
    };
  },
  props: ["transData", "popType"],
  created() {
    if (this.transData.img) {
      this.fileList = [
        {
          name: this.ruleForm.title,
          url: "http://localhost:3000" + this.transData.img
        }
      ];
    } else {
      this.fileList = [];
    }
  },
  watch: {
    transData: {
      handler(newVal, oldVal) {
        if (newVal.img) {
          if (typeof newVal.img == "string") {
            this.fileList = [
              {
                name: newVal.title,
                url: "http://localhost:3000" + this.transData.img
              }
            ];
          }
        } else {
          this.fileList = [];
        }
        this.ruleForm = newVal;
      },
      deep: true
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          // 校验成功
          console.log(this.ruleForm);
          
          let formData=new FormData();
          for (let key in this.ruleForm) {
            formData.append(key,this.ruleForm[key])
          }
          if (this.popType == "修改") {
          //   //修改
            this.$http
              .post("/banneredit", formData)
              .then(res => {
                // this.$router.go(0);
              })
              .catch(err => {
                console.log(err);
              });
          } else {
          //   // 新增
            this.$http
              .post("/banneradd", formData)
              .then(res => {
                // this.$router.go(0);
              })
              .catch(err => {
                console.log(err);
              });
          }
        } else {
          // 检验失败
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleChange(file, fileList) {
      // 当文件变化时，将最终的值传给img
      this.ruleForm.img = file.raw;
    },
    limitImg() {
      this.$message.warning("轮播图图片最多添加1个");
    }
  }
};
</script>
<style lang="" >
.info {
  width: 100%;
  height: auto;
  background: #fff;
}
.box2 {
  width: auto;
  margin: 20px 20px 0 0;
}
.el-dialog__body {
  padding: 0;
}
.el-dialog__footer {
  padding: 0px 20px 10px;
}
.el-dialog__wrapper {
  top: -70px;
}
</style>